package mi.payment.ui.admin;

import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.CompoundButton;
import android.widget.TextView;

import mi.payment.R;
import mi.payment.controller.BaseQpsSettingController;
import mi.payment.controller.QpsSettingController;
import mi.payment.settings.QPSParamsOperation;
import mi.payment.settings.QPSParamsSetting;
import mi.payment.ui.base.ParentActivity;
import mi.payment.utils.MoneyUtils;
import sunmi.sunmiui.button.ButtonSwitch;
import sunmi.sunmiui.dialog.DialogCreater;
import sunmi.sunmiui.dialog.EditTextDialog;
import sunmi.sunmiui.title.Title;
import sunmi.sunmiui.utils.LogUtil;

/**
 * 非接业务参数控制
 * Created by zdy on 2017/8/16.
 */

public class QpsTransactionSettingActivity extends ParentActivity {

    private View qpsCDCVM, qpsFreePsw, qpsBinA, qpsBinB, qpsBinC, qpsFreeSign;
    private View qpsFreePswLimit, qpsFreeSignLimit;
    private ButtonSwitch qpsCDCVMButtonSwitch, qpsFreePswButtonSwitch, qpsBinAButtonSwitch, qpsBinBButtonSwitch, qpsBinCButtonSwitch, qpsFreeSignButtonSwitch;
    private TextView qpsFreePswLimitNumber, qpsFreeSignLimitNumber;
    private QPSParamsOperation qpsParamsOperation;
    private EditTextDialog mEditTextDialog;
    private boolean canAlter;
    private BaseQpsSettingController qpsSettingController;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qps_transaction_setting);
        qpsParamsOperation = QPSParamsOperation.getInstance();
        qpsSettingController = new QpsSettingController(this);
        canAlter = qpsSettingController.canAlter();
        initView();
        setListener();
        updateUI();
    }

    private void initView() {
        Title mTitle;
        TextView leftText;
        mTitle = (Title) findViewById(R.id.qps_transaction_setting_title);
        mTitle.setButtonVisibility(false, true, true, true);
        mTitle.setHeadBgColor(getResources().getColor(R.color.colorBar));
        mTitle.setTitleText(getResources().getString(R.string.qps_setting));
        mTitle.setMenuClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                QpsTransactionSettingActivity.this.finish();
            }
        }, null, null, null);

        qpsFreePsw = findViewById(R.id.qps_free_psw);
        qpsFreePswButtonSwitch = (ButtonSwitch) qpsFreePsw.findViewById(R.id.set_switch);

        leftText = (TextView) qpsFreePsw.findViewById(R.id.switch_item_text);
        leftText.setText(qpsSettingController.setByPassName());

        qpsCDCVM = findViewById(R.id.qps_cdcvm);
        qpsCDCVMButtonSwitch = (ButtonSwitch) qpsCDCVM.findViewById(R.id.set_switch);
        leftText = (TextView) qpsCDCVM.findViewById(R.id.switch_item_text);
        leftText.setText(getResources().getString(R.string.qps_cdcvm));

        qpsBinA = findViewById(R.id.qps_bin_a);
        qpsBinAButtonSwitch = (ButtonSwitch) qpsBinA.findViewById(R.id.set_switch);
        leftText = (TextView) qpsBinA.findViewById(R.id.switch_item_text);
        leftText.setText(getResources().getString(R.string.qps_bin_a));

        qpsBinB = findViewById(R.id.qps_bin_b);
        qpsBinBButtonSwitch = (ButtonSwitch) qpsBinB.findViewById(R.id.set_switch);
        leftText = (TextView) qpsBinB.findViewById(R.id.switch_item_text);
        leftText.setText(getResources().getString(R.string.qps_bin_b));

        qpsBinC = findViewById(R.id.qps_bin_c);
        qpsBinCButtonSwitch = (ButtonSwitch) qpsBinC.findViewById(R.id.set_switch);
        leftText = (TextView) qpsBinC.findViewById(R.id.switch_item_text);
        leftText.setText(getResources().getString(R.string.qps_bin_c));

        if (!qpsSettingController.showBinCSwitchButton()) {
            qpsBinC.setVisibility(View.GONE);
        }

        qpsFreeSign = findViewById(R.id.qps_free_sign);
        qpsFreeSignButtonSwitch = (ButtonSwitch) qpsFreeSign.findViewById(R.id.set_switch);
        leftText = (TextView) qpsFreeSign.findViewById(R.id.switch_item_text);
        leftText.setText(getResources().getString(R.string.qps_free_sign));


        qpsFreePswLimit = findViewById(R.id.qps_free_psw_limit);
        leftText = (TextView) qpsFreePswLimit.findViewById(R.id.item_left_text);
        qpsFreePswLimitNumber = (TextView) qpsFreePswLimit.findViewById(R.id.item_right_text);
        leftText.setText(getResources().getString(R.string.qps_free_psw_limit));

        qpsFreeSignLimit = findViewById(R.id.qps_free_sign_limit);
        leftText = (TextView) qpsFreeSignLimit.findViewById(R.id.item_left_text);
        qpsFreeSignLimitNumber = (TextView) qpsFreeSignLimit.findViewById(R.id.item_right_text);
        leftText.setText(getResources().getString(R.string.qps_free_sign_limit));


    }

    private void setListener() {
        qpsFreePswLimit.setOnClickListener(this);
        qpsFreeSignLimit.setOnClickListener(this);
        qpsCDCVM.setOnClickListener(this);
        qpsFreePsw.setOnClickListener(this);
        qpsBinA.setOnClickListener(this);
        qpsBinB.setOnClickListener(this);
        qpsBinC.setOnClickListener(this);
        qpsFreeSign.setOnClickListener(this);


        // --  button switch clickListener    -- //
        final QPSParamsSetting qpsParamsSetting = qpsParamsOperation.getQPSParamsSettings();
        qpsFreePswButtonSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (canAlter) {
                    qpsParamsOperation.saveQpsParamNeedNoPswId(isChecked);
                } else {
                    buttonView.setChecked(qpsParamsSetting.isQpsParamNeedNoPswId());

                }
            }
        });
        qpsCDCVMButtonSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (canAlter) {
                    qpsParamsOperation.saveQpsParamCDCVMId(isChecked);
                } else {
                    buttonView.setChecked(qpsParamsSetting.isQpsParamCDCVMId());

                }
            }
        });
        qpsBinAButtonSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (canAlter) {
                    qpsParamsOperation.saveQpsParamBinAId(isChecked);
                } else {
                    buttonView.setChecked(qpsParamsSetting.isQpsParamBinAId());

                }
            }
        });
        qpsBinBButtonSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (canAlter) {
                    qpsParamsOperation.saveQpsParamBinBId(isChecked);
                } else {
                    buttonView.setChecked(qpsParamsSetting.isQpsParamBinBId());

                }
            }
        });
        qpsBinCButtonSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (canAlter) {
                    qpsParamsOperation.saveQpsParamBinCId(isChecked);
                } else {
                    buttonView.setChecked(qpsParamsSetting.isQpsParamBinCId());

                }
            }
        });
        qpsFreeSignButtonSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (canAlter) {
                    qpsParamsOperation.saveQpsParamNeedNoSignId(isChecked);
                } else {
                    buttonView.setChecked(qpsParamsSetting.isQpsParamNeedNoSignId());
                }
            }
        });

    }


    @Override
    public void onClick(View v) {
        if (canAlter) {
            switch (v.getId()) {
                case R.id.qps_free_psw_limit:
                    mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                            getString(R.string.save), getString(R.string.qps_free_psw_limit), new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    mEditTextDialog.dismiss();
                                }
                            }, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (checkEmpty(mEditTextDialog.getText())) {
                                        if (Integer.parseInt(mEditTextDialog.getText()) == 0) {
                                            showToast(getResources().getString(R.string.error_input));
                                        } else {
                                            long money = MoneyUtils.stringMoney2LongCent(mEditTextDialog.getText());
                                            qpsParamsOperation.saveQpsParamNeedNoPswLimit(money);
                                            mEditTextDialog.dismiss();
                                            updateUI();
                                        }
                                    }
                                }
                            }, new DialogInterface.OnShowListener() {
                                @Override
                                public void onShow(DialogInterface dialog) {
                                    mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                                    mEditTextDialog.setMaxLength(9999);
                                    showSoftKeyboard(mEditTextDialog);
                                }
                            });

                    mEditTextDialog.show();
                    break;
                case R.id.qps_free_sign_limit:
                    mEditTextDialog = DialogCreater.createEditTextDialog(this, getString(R.string.cancel),
                            getString(R.string.save), getString(R.string.qps_free_sign_limit), new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    mEditTextDialog.dismiss();
                                }
                            }, new View.OnClickListener() {
                                @Override
                                public void onClick(View view) {
                                    if (checkEmpty(mEditTextDialog.getText())) {
                                        if (Integer.parseInt(mEditTextDialog.getText()) == 0) {
                                            showToast(getResources().getString(R.string.error_input));
                                        } else {
                                            long money = MoneyUtils.stringMoney2LongCent(mEditTextDialog.getText());
                                            qpsParamsOperation.saveQpsParamNeedNoSignLimit(money);
                                            mEditTextDialog.dismiss();
                                            updateUI();
                                        }
                                    }
                                }
                            }, new DialogInterface.OnShowListener() {
                                @Override
                                public void onShow(DialogInterface dialog) {
                                    mEditTextDialog.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
                                    mEditTextDialog.setMaxLength(9999);
                                    showSoftKeyboard(mEditTextDialog);
                                }
                            });

                    mEditTextDialog.show();

                    break;

                case R.id.qps_free_psw:
                    qpsFreePswButtonSwitch.setOnCheck(!qpsFreePswButtonSwitch.isChecked());
                    break;
                case R.id.qps_cdcvm:
                    qpsCDCVMButtonSwitch.setOnCheck(!qpsCDCVMButtonSwitch.isChecked());
                    break;
                case R.id.qps_bin_a:
                    qpsBinAButtonSwitch.setOnCheck(!qpsBinAButtonSwitch.isChecked());
                    break;
                case R.id.qps_bin_b:
                    qpsBinBButtonSwitch.setOnCheck(!qpsBinBButtonSwitch.isChecked());
                    break;
                case R.id.qps_bin_c:
                    qpsBinCButtonSwitch.setOnCheck(!qpsBinCButtonSwitch.isChecked());
                    break;
                case R.id.qps_free_sign:
                    qpsFreeSignButtonSwitch.setOnCheck(!qpsFreeSignButtonSwitch.isChecked());
                    break;
                default:
                    break;
            }
        } else {
            showToast(getString(R.string.qps_can_not_Alter));
        }
    }

    private void updateUI() {
        QPSParamsSetting qpsParamsSetting = qpsParamsOperation.getQPSParamsSettings();
        qpsFreePswButtonSwitch.setOnCheck(qpsParamsSetting.isQpsParamNeedNoPswId());
        qpsCDCVMButtonSwitch.setOnCheck(qpsParamsSetting.isQpsParamCDCVMId());
        LogUtil.e("zdy", "   ----   " + qpsParamsSetting.isQpsParamCDCVMId());
        qpsBinAButtonSwitch.setOnCheck(qpsParamsSetting.isQpsParamBinAId());
        qpsBinBButtonSwitch.setOnCheck(qpsParamsSetting.isQpsParamBinBId());
        qpsBinCButtonSwitch.setOnCheck(qpsParamsSetting.isQpsParamBinCId());
        qpsFreeSignButtonSwitch.setOnCheck(qpsParamsSetting.isQpsParamNeedNoSignId());
        qpsFreePswLimitNumber.setText(MoneyUtils.longCent2DoubleMoneyStr(qpsParamsSetting.getQpsParamNeedNoPswLimit()));
        LogUtil.e("zdy", "   ====   " + qpsParamsSetting.getQpsParamNeedNoPswLimit());
        qpsFreeSignLimitNumber.setText(MoneyUtils.longCent2DoubleMoneyStr(qpsParamsSetting.getQpsParamNeedNoSignLimit()));
    }

}
